package com.terminee.model;

import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PrimaryKey;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.Persistent;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Text;

/**
 * A Term is a frequently used term they have different meanings in different
 * contexts.
 * 
 * @author potlow
 */
@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable="true")
public class Term {

	@PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Key key;
	
	// Term
    @Persistent
    private String word = "";
    
    // a definition of term
    @Persistent
    private Text definition;
    
    /**
     * Constructor
     * @param word term
     * @param definition definition of term
     */
    public Term(String word, String definition) {
        this.word = word;
        this.definition = new Text(definition);
    }
    
    /**
     * Constructor
     * Converts a client-side term to term ready for db
     * 
     * @param clientTerm
     */
    public Term(ClientTerm clientTerm) {
    	this.word = clientTerm.getWord();
    	this.definition = new Text(clientTerm.getDefinition());
    }

    /**
     * Default constructor
     */
    public Term() {
    }

	public Key getKey() {
		return key;
	}

	public void setKey(Key key) {
		this.key = key;
	}

	public String getWord() {
		return word;
	}

	public void setWord(String word) {
		this.word = word;
	}

	public Text getDefinition() {
		return definition;
	}

	public void setDefinition(Text definition) {
		this.definition = definition;
	}
	
	public ClientTerm getClientTerm() {
		ClientTerm clientTerm = new ClientTerm();
        clientTerm.setWord(word);
        clientTerm.setDefinition(definition.getValue());
        clientTerm.setKey(KeyFactory.keyToString(key));

        return clientTerm;
	}
}
